<?php
/**
 * Created by PhpStorm.
 * User: weblinuxgame
 * Date: 2017-09-11
 * Time: 11:57
 */
namespace EpgApp\modules\sdks\Image\base\gbclass;

use EpgApp\modules\sdks\Image\base\ImageException;
use EpgApp\modules\sdks\Image\base\functions;

include_once(dirname(__DIR__).DIRECTORY_SEPARATOR.'functions'.DIRECTORY_SEPARATOR.'imagecreatefrombmp.php');

class ImageResource
{
    private static $resource ;
    public function __construct($filename)
    {
        $this->setResource($filename);
    }

    public static function setResource($filename='')
    {
        /**
         * @var ImageResource|String $filename
         * ***/
        if(self::isImageResource($filename))
        {
           if(!($filename->getResource()))
           {
               return false;
           }
          return   empty(self::$resource)? self::$resource = $filename->getResource(): false;
        }
        if(is_string($filename)&&!empty($filename))
        {
            ImageException::isImageFile($filename) && ImageException::file_exists($filename) ;
           return empty(self::$resource)? self::$resource = self::openResource($filename) : false;
        }
        return false;
    }

    public static function getResource()
    {
        return empty(self::$resource) ? false : self::$resource ;
    }

    public static function openResource($fileName)
    {
          if(self::isImageResource($fileName))
          {
              /**
               *@var ImageResource|String $fileName
               */
              $r = $fileName->getResource();
              if(empty($r))
              {
                  return null ;
              }
              return $r;
          }
          if(is_string($fileName) && filter_var($fileName,FILTER_VALIDATE_URL,FILTER_FLAG_PATH_REQUIRED ))
          {
              $fileInfo = array('extension'=>'xbm');
          }
          else
          {
              ImageException::file_exists($fileName) && ImageException::isImageFile($fileName);
              $fileInfo = pathinfo($fileName);
          }
          if(empty($fileInfo['extension']))
          {
              return false;
          }
          switch ($fileInfo['extension'])
          {
              case 'png':
                    return \imagecreatefrompng($fileName);
                    break;
              case 'jpg':
                    return \imagecreatefromjpeg($fileName);
                    break;
              case 'bmp':
                  return functions\imagecreatefrombmp($fileName);
                  break;
              case 'xbm':
                  return \imagecreatefromxbm($fileName);
                  break;
              case 'xpm':
                  return \imagecreatefromxpm($fileName);
                  break;
              case 'gif':
                  return \imagecreatefromgif($fileName);
                  break;
              case 'wbmp':
                  return \imagecreatefromwbmp($fileName);
                  break;
              case 'svg':
                  return \imagecreatefromxpm($fileName);
                  break;
              default:
                  break;
          }
        return null ;
    }

    public static function isImageResource($obj=null)
    {
        if(empty($obj) || is_string($obj))
        {
            return false;
        }

        if(is_object($obj) && $obj instanceof ImageResource)
        {
            return true;
        }
        return false;
    }

}